package qq2564874169.tuaotuao.fx.app;

import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.UUID;

public class RequestInterceptor implements HandlerInterceptor {
    private static final String key = UUID.randomUUID().toString();
    private FxSpringApp app;

    public RequestInterceptor(FxSpringApp app) {
        this.app = app;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        RequestEventArgs args = new RequestEventArgs(request, response);
        request.setAttribute(key, args);
        app.getMvcEvents().onReuqestBefore(app, args);
        return args.cancel == false;
    }

    static RequestEventArgs getArgs(ServletRequest request) {
        return (RequestEventArgs) request.getAttribute(key);
    }
}
